package com.smartthinking.destinomusical;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

import com.smartthinking.destinomusical.adapter.PlaylistAdapter;
import com.smartthinking.destinomusical.adapter.SongListAdapter;
import com.smartthinking.destinomusical.base.BaseActivity;
import com.smartthinking.destinomusical.db.ControlBD;
import com.smartthinking.destinomusical.obj.Cancion;

public class ListaReproduccion extends BaseActivity {
	private ControlBD bd;
	PlaylistAdapter songListAdapter;
	EditText  inputSearch;
	List<Cancion> songList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playlist);

		setTopBar(0, 0, R.string.boton_playlist);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		bd = new ControlBD(this);
		
		refresh();
	}
	
	private void refresh(){
		songList = bd.selectCanciones();
		songListAdapter = new PlaylistAdapter(ListaReproduccion.this, songList);

		ListView lstreproduccion = (ListView) findViewById(R.id.playlist);
		lstreproduccion.setAdapter(songListAdapter);
		lstreproduccion.setOnItemClickListener(itemSelected);
		inputSearch = (EditText) findViewById(R.id.search);
		inputSearch.addTextChangedListener(new TextWatcher() {

		    @Override
		    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
		        // When user changed the Text
		    	songListAdapter.getFilter().filter(cs);
		    }
		    @Override
		    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) { }
		    @Override
		    public void afterTextChanged(Editable arg0) {}
		});
	}
	
	Cancion cancion = null;
	OnItemClickListener itemSelected = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long id) {
			cancion = (Cancion)songList.get(position);
			Intent intent = new Intent(ListaReproduccion.this, MenuSong.class);
			intent.putExtra("usuario", usuario);
			intent.putExtra("cancion", cancion);
			intent.putExtra("position", position);
			intent.putExtra("lista","si");
			ArrayList <Cancion>canciones= (ArrayList<Cancion>) songList;
			intent.putParcelableArrayListExtra("canciones", canciones);
			//startActivity(intent);
			startActivityForResult(intent,100);
		}
	};

	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		switch(requestCode) {
			case 100:
				if (resultCode == RESULT_OK) {
					String ref = data.getStringExtra("Refresca");
					if (ref.equals("ok")){
						refresh();
					}
					break;
				}
		}
     }//onresult
}
